$(document).ready(init);

/**
 * 初始化
 */
function init() {
	$('#search').click(function() {
		
		var params = {};
		
		var deptNo = $.trim($('#deptNo').val());
		if ('' != deptNo) {
			params['iDepartment.deptNo'] = deptNo;
		}
		
		var deptName = $.trim($('#deptName').val());
		if ('' != deptName) {
			params['iDepartment.deptName'] = deptName;
		}
		
		showDepartment(params);
	});
		
	showDepartment({});
}

/**
 * 机构信息列表
 */
function showDepartment(params) {
	$('#department').datagrid({
	    title:'',
	    iconCls:'icon-save',
	    singleSelect:true,
	    url:'department!showDepartMentIncludeParent.action',
	    idField: 'deptID',
	    rownumbers : true,
	    queryParams: params,
	    columns:[[
	        {field:'deptName',title:'机构名称',width:fixWidth(0.11)},
	        {field:'deptNo', title:'编号', width:fixWidth(0.106)},
	        {field:'parentName',title:'从属机构',width:fixWidth(0.1)},
	        {field:'empName',title:'机构组长',width:fixWidth(0.1)},
	        {field:'payCode',title:'付款账号',width:fixWidth(0.1)},
	        {field:'phone',title:'电话',width:fixWidth(0.1)},
	        {field:'rebate',title:'折扣率(%)',width:fixWidth(0.07)},
	        {field:'balance', title:'余额(元)',width:fixWidth(0.06)},
	        {field:'facilityExtent', title:'透支额度(元)',width:fixWidth(0.07)},
	        {field:'deptID',title:'操作',width:fixWidth(0.15),align:'center',
	        	    formatter : function(value, row, index) {
						var e = '<a href="#" style="color:red;text-decoration:none;" onclick="editrow('+ index + ',\'' + value +'\')">编辑</a> ';
						var f = '<a href="#" style="color:red;text-decoration:none;" onclick="showDetail('+ index + ',\'' + value +'\')">详情</a> ';
						var g = '<a href="#" style="color:red;text-decoration:none;" onclick="docharge('+ index + ',\'' + value +'\')">充值</a> ';
						
						return e + '&nbsp;&nbsp;&nbsp;' + f + '&nbsp;&nbsp;&nbsp;' + g;
					}
			}
	    ]],
	    pagination:true,
	    fitColumns:true
	});
}

/**
 * 编辑机构信息
 */
function editrow(index,value){
	var title='编辑机构信息';
	
	var url = 'department!getTheDepartmentVo.action?deptID=' + value + '&method=update' + '&time=' + new Date();
	var content = '<iframe scrolling="auto" frameborder="0"  src="' + url + '" style="width:100%;height:100%;"></iframe>';
	
	if (parent.$('#content').tabs('exists', title)){
		parent.$('#content').tabs('update', {
			tab : parent.$('#content').tabs('getTab', title),
			options : {
				content:content
			}
		});
		
        parent.$('#content').tabs('select', title);
    } else {
        parent.$('#content').tabs('add',{
            title:title,
            content:content,
            closable:true
        });
    }
}

/**
 * 显示机构信息
 */
function showDetail(index,value) {
	var title='机构信息详情';
	
	var url = 'department!getTheDepartmentVo.action?deptID=' + value + '&method=query' + '&time=' + new Date();
    var content = '<iframe scrolling="auto" frameborder="0"  src="'+ url +'" style="width:100%;height:100%;"></iframe>';
	
	if (parent.$('#content').tabs('exists', title)){
		parent.$('#content').tabs('update', {
		  	tab: parent.$('#content').tabs('getTab', title),
			options:{
				content:content
		  	}
		});
		
        parent.$('#content').tabs('select', title);
    } else {
        parent.$('#content').tabs('add',{
            title:title,
            content:content,
            closable:true
        });
    }
}


/**
 * 显示机构信息
 */
function docharge(index,value) {
	var title='充值操作';
	
	var url = 'department!goToCharge.action?deptID=' + value  + '&time=' + new Date();
    var content = '<iframe scrolling="auto" frameborder="0"  src="'+ url +'" style="width:100%;height:100%;"></iframe>';
	
	if (parent.$('#content').tabs('exists', title)){
		parent.$('#content').tabs('update', {
		  	tab: parent.$('#content').tabs('getTab', title),
			options:{
				content:content
		  	}
		});
		
        parent.$('#content').tabs('select', title);
    } else {
        parent.$('#content').tabs('add',{
            title:title,
            content:content,
            closable:true
        });
    }
}
